knitr::opts_chunk$set(
  warning = FALSE, 
  message = FALSE)


library(tidyverse)
## -- Attaching packages ------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ---------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(leaflet)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(dplyr)

Electricity usage in the Bay Area and the impacts of COVID

On this page, we explore how electricity use in the Bay Area varies over space and time, with a particular emphasis on the impacts of COVID on residential and commercial electricity use.

First, let’s take a look at total energy use across the Bay Area, separated by residential and commercial uses.

pge_res_chart <-
  pge_bay_total %>%
  filter(CUSTOMERCLASS %in% c('Elec- Residential','Gas- Residential')) %>%
  ggplot() + 
  geom_bar(
    aes(
      x = TIME,
      y = TOTALKBTU,
      fill = CUSTOMERCLASS
    ),
    stat = "identity",
    position = "dodge"
  ) + 
  labs(
    x = "Time",
    y = "kBTU",
    title = "Bay Area Monthly Residential Energy Usage, 2017-2020",
    fill = "Electricity Type"
  ) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 8)) +
  scale_x_date(date_breaks = "2 month",date_labels = "%b, %Y")

We can see a clear annual cycle in residential gas usage, with a maximum in wintertime and a minimum in summertime. This makes sense given than gas is used for heating in most homes.

There is a smaller but clearly semiannual cycle in residential electricity, peaking in both winter and summer. I expect this is due to residential heating and cooling from electrical systems such as air conditioning units. The other reason electricity would go up in winter is from more lighting in the mornings and evenings due to less hours of sunlight.

pge_com_chart <-
  pge_bay_total %>%
  filter(CUSTOMERCLASS %in% c('Elec- Commercial','Gas- Commercial')) %>%
  
  ggplot() + 
  geom_bar(
    aes(
      x = TIME,
      y = TOTALKBTU,
      fill = CUSTOMERCLASS
    ),
    stat = "identity",
    position = "dodge"
  ) + 
  labs(
    x = "Time",
    y = "kBTU",
    title = "Bay Area Monthly Commercial Energy Usage, 2017-2020",
    fill = "Electricity Type"
  ) +
  theme(axis.text.x = element_text(angle = 60, hjust = 1, size = 8)) +
  scale_x_date(date_breaks = "2 month",date_labels = "%b, %Y")

pge_res_chart

pge_com_chart

Commercial electricity and gas consumption are out of phase, with largest electricity use and lowest gas use in summer. It makes sense that heating demands on gas use continue to peak in winter months, but as people leave offices at night, electricity demands would not increase in winter.

I wonder what caused the large peak in September, 2017 in both residential and commercial electrical and gas use. It must be a glitch in the data, as that much power use would have blown up the entire electrical system. It may be that they changed their recording system, like going from the start of the month to the end of the month or something like that.

We can also observe from the time series that commercial gas use has been increasing slowly over time - and that there is a visible decline in commercial electricity use in the spring of 2020.

Let’s take a closer look at how COVID has changed patterns of energy use across the Bay Area. Here’s what average springtime residential electricity use looked like on average from 2017 to 2019, before the pandemic hit:

avg_res_elec_preCOVID <-
  avg_res_elec_preCOVID %>%
  left_join(
    bay_zips %>% select(GEOID10),
    by = c("ZIPCODE" = "GEOID10")
  ) %>%
  st_as_sf() %>%
  st_transform(4326) %>%
  filter( !is.na(PREAVG))

clim <- 3000
  
res_pal <- colorNumeric(
  palette = "Spectral",
  domain = avg_res_elec_preCOVID$PREAVG,
  reverse = T
)

# map it out
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    data = avg_res_elec_preCOVID,
    fillColor = ~res_pal(PREAVG),
    color = "white",
    opacity = 0.5,
    fillOpacity = 0.5,
    weight = 1,
    label = ~paste0(
      round(PREAVG,2), " kBTU/home used in",
      ZIPCODE
    ),
    highlightOptions = highlightOptions(
      weight = 2,
      opacity = 1
    ) 
  ) %>%
  addLegend(
    data = avg_res_elec_preCOVID,
    pal = res_pal,
    values = avg_res_elec_preCOVID$PREAVG,
    title = "Average Residential<br> Electricity Use <br> (kBTU), Pre-COVID"
  )  

Average electricity use varies from about 800 to 3,000 kBTU, with a few outlier counties that may be issues with the PG&E data. The ultra-wealthy city of Atherton sticks out like a sore thumb. Palo Alto is on its own electrical grid, and so doesn’t show up in PG&E’s data.

There seems to be a slight trend towards higher electricity use with distance from the Bay. The Bay naturally helps regulate temperatures, so less heating and cooling are necessary for houses close to the water.

Now look at the change in the average during the pandemic.

clim <- 500
  
res_pal <- colorNumeric(
  palette = "RdBu",
  domain = c(-clim, clim),
  reverse = TRUE
)

# map it out
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    data = avg_res_elec_change,
    fillColor = ~res_pal(CHANGE),
    color = "white",
    opacity = 0.5,
    fillOpacity = 0.5,
    weight = 1,
    label = ~paste0(
      round(CHANGE,2), " kBTU change in",
      ZIPCODE
    ),
    highlightOptions = highlightOptions(
      weight = 2,
      opacity = 1
    ) 
  ) %>%
  addLegend(
    data = res_elec_change,
    pal = res_pal,
    values = c(-clim, clim),
    title = "Change in Average Residential<br> Electricity Use (kBTU),<br>Post-Pre COVID"
  )  

Other than Napa, Sonoma and some other counties further north along the coast, almost the entire Bay Area shows increases in per capita residential electricity use. Some of the greatest increases are in the delta area, near Antioch, Oakley and Brentwood. In Atherton, electricity use has increased by an additional 287 kBTU.

We can also look at the data in terms of the changes in total electricity use by zipcode. This gives a better sense of the load changes on the grid, and how PG&E has to adapt.

res_pal <- colorNumeric(
  palette = "RdBu",
  domain = c(-9000000, 9000000),
  reverse = TRUE
)

# map it out
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    data = res_elec_change %>%
      filter(!is.na(CHANGE)),
    fillColor = ~res_pal(CHANGE),
    color = "white",
    opacity = 0.5,
    fillOpacity = 0.5,
    weight = 1,
    label = ~paste0(
      round(CHANGE,2), " kBTU change in",
      ZIPCODE
    ),
    highlightOptions = highlightOptions(
      weight = 2,
      opacity = 1
    ) 
  ) %>%
  addLegend(
    data = res_elec_change,
    pal = res_pal,
    values = c(-9000000, 9000000),
    title = "Change in Residential<br> Electricity Use (kBTU),<br>Post-Pre COVID"
  )  

The Antioch area is particularly noticeable in this rendition.

The next map shows the % change in residential electricity use:

res_pal <- colorNumeric(
  palette = "RdBu",
  domain = c(-30, 30),
  reverse = TRUE
)

# map it out
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    data = res_elec_change %>%
      filter(!is.na(NORM_CHANGE)),
    fillColor = ~res_pal(NORM_CHANGE),
    color = "white",
    opacity = 0.5,
    fillOpacity = 0.5,
    weight = 1,
    label = ~paste0(
      round(NORM_CHANGE,2), "% change in",
      ZIPCODE
    ),
    highlightOptions = highlightOptions(
      weight = 2,
      opacity = 1
    ) 
  ) %>%
  addLegend(
    data = res_elec_change,
    pal = res_pal,
    values = c(-30,30),
    title = "% Change in Residential<br> Electricity Use,<br>Post-Pre COVID"
  )  

This last map of % change in total residential electricity use makes a few additional areas stick out. SOMA in SF, Redwood City, and Mountain View all have relatively high % changes. I expect that this is because of the large number of people working in tech who live in these areas and transitioned to working from home during the pandemic. Additionally, the counties along the coast in the North Bay have been grayed out in this rendition on account of having significantly higher % changes in residential electricity (as much as 118%). This area is quite sparsely populated, but may have been an important destination for people escaping the bigger cities during COVID.

In contrast, here is the % change in commercial electricity use over the same time. Notice how some zipcodes have a 100% decrease in commercial electricity, implying that commercial use has dropped to zero.

com_elec_preCOVID <- 
  pge_bay_elec %>%
  filter(CUSTOMERCLASS %in% c('Elec- Commercial')) %>%
  filter(YEAR <= 2019) %>%
  filter(MONTH %in% c(4,5,6)) %>%
  group_by(ZIPCODE) %>%
  summarize(
    PREKBTU = 
      mean(
        TOTALKBTU,
        na.rm = T
        )
  )

com_elec_postCOVID <- 
  pge_bay_elec %>%
  filter(CUSTOMERCLASS %in% c('Elec- Commercial')) %>%
  filter(YEAR == 2020) %>%
  filter(MONTH %in% c(4,5,6)) %>%
  group_by(ZIPCODE) %>%
  summarize(
    POSTKBTU = 
      mean(
        TOTALKBTU,
        na.rm = T
        )
  )

com_elec_change <-
  left_join(com_elec_preCOVID,com_elec_postCOVID) %>%
  right_join(
    bay_zips %>% select(GEOID10),
    by = c("ZIPCODE" = "GEOID10")
  ) %>%
  st_as_sf() %>%
  st_transform(4326)


com_elec_change$CHANGE <- 
  com_elec_change$POSTKBTU - com_elec_change$PREKBTU
com_elec_change$NORM_CHANGE <- 
  (com_elec_change$POSTKBTU - com_elec_change$PREKBTU)/com_elec_change$PREKBTU*100

rm(com_elec_postCOVID, com_elec_preCOVID)
  
res_pal <- colorNumeric(
  palette = "RdBu",
  domain = c(-100, 100),
  reverse = TRUE
)


# map it out
leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(
    data = com_elec_change %>%
      filter(!is.na(NORM_CHANGE)),
    fillColor = ~res_pal(NORM_CHANGE),
    color = "white",
    opacity = 0.5,
    fillOpacity = 0.5,
    weight = 1,
    label = ~paste0(
      round(NORM_CHANGE,2), "% change in",
      ZIPCODE
    ),
    highlightOptions = highlightOptions(
      weight = 2,
      opacity = 1
    ) 
  ) %>%
  addLegend(
    data = com_elec_change,
    pal = res_pal,
    values = c(-100,100),
    title = "% Change in Commercial<br> Electricity Use,<br>Post-Pre COVID"
  )  

It’s important to acknowledge the limitations to this analysis. There are certain counties that PG&E does not have data for, like Palo Alto for example. I’m curious about why Alameda and Santa Clara counties (among others) are also missing.

This also assumes that no other major changes occurred between 2017 and 2020 that would affect energy distribution around the Bay. In reality, people move in and out of the area, between zipcodes, and businesses move as well. Additionally, weather and climate factors may affect electricity and gas usage across different years. For example, a particularly cold spell one year could increase that year’s average electricity use and throw off the change calculations. It is also notable how the various metrics for electricity use and change (per household, total, percent difference) alter the maps in sometimes important ways. This brings into question how these statistics should be portrayed, and the different management implications each map suggests.